package com.expert.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.expert.constant.RabbitMQConstant;
import com.expert.context.BaseContext;
import com.expert.enums.NoticeType;
import com.expert.pojo.Expert;
import com.expert.pojo.Notice;
import com.expert.pojo.NoticeUser;
import com.expert.pojo.Project;
import com.expert.service.*;
import com.expert.util.EmailMessageUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName: RabbitMQListener
 * @Author: cxs
 * @Date: 2024/03/26 19:00
 * @Description:
 **/
@Configuration
public class RabbitMQListener {
    @Autowired
    ExpertService expertService;
    @Autowired
    ProjectService projectService;
    @Autowired
    NoticeService noticeService;
    @Autowired
    MailService mailService;
    @Autowired
    NoticeUserService noticeUserService;
    /**
     * @param msg     接受的消息
     * @param message
     * @param channel
     */
    @RabbitListener(queues = RabbitMQConstant.QUEUE_SMS)
    public void smsMessage(String msg, Message message, Channel channel) {
        System.out.println("发送短信：" + msg);
    }

    /**
     * @param msg     接受的消息
     * @param message
     * @param channel
     */
    @RabbitListener(queues = RabbitMQConstant.QUEUE_EMAIL)
    public void emailMessage(String msg, Message message, Channel channel) {
        JSONObject jsonObject = JSON.parseObject(msg);
        Object expertIdObject = jsonObject.get("expertId");
        Object projectIdObject = jsonObject.get("projectId");
        Long expertId = Long.parseLong(expertIdObject.toString());
        Long projectId = Long.parseLong(projectIdObject.toString());
        Expert expert = this.expertService.getById(expertId);
        Project project=this.projectService.getById(projectId);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format = dtf.format(project.getProjectReviewTime());
        EmailMessageUtil emailMessageUtil=new EmailMessageUtil(expert.getExpertName(),project.getProjectName(),project.getProjectLocation(),format);
        this.mailService.sendTextMailMessage(expert.getExpertEmail(),project.getProjectName()+"评审邀请",emailMessageUtil.message());
    }

    /**
     * @param msg     接受的消息
     * @param message
     * @param channel
     */
    @RabbitListener(queues = RabbitMQConstant.QUEUE_SYS)
    public void sysMessage(String msg, Message message, Channel channel) {
        JSONObject jsonObject = JSON.parseObject(msg);
        Object expertIdObject = jsonObject.get("expertId");
        Object projectIdObject = jsonObject.get("projectId");
        Long expertId = Long.parseLong(expertIdObject.toString());
        Long projectId = Long.parseLong(projectIdObject.toString());
        Expert expert = this.expertService.getById(expertId);
        Project project=this.projectService.getById(projectId);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format = dtf.format(project.getProjectReviewTime());
        Notice notice=new Notice();
        notice.setNoticeType(NoticeType.USER.getCode());
        notice.setNoticeTitle(project.getProjectName()+"评审邀请");
        EmailMessageUtil emailMessageUtil=new EmailMessageUtil(expert.getExpertName(),project.getProjectName(),project.getProjectLocation(),format);
        notice.setNoticeContent(emailMessageUtil.message());
        setNoticeInfo(expert, notice);
    }


    @RabbitListener(queues = RabbitMQConstant.KEY_CANCEL_PROJECT_EMAIL)
    public void cancelProjectMessage(String msg,Message message,Channel channel){
        JSONObject jsonObject = JSON.parseObject(msg);
        Object expertIdObject = jsonObject.get("expertId");
        Object projectIdObject = jsonObject.get("projectId");
        Long expertId = Long.parseLong(expertIdObject.toString());
        Long projectId = Long.parseLong(projectIdObject.toString());
        Expert expert = this.expertService.getById(expertId);
        Project project=this.projectService.getById(projectId);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format = dtf.format(project.getProjectReviewTime());
        String cancelMessge = EmailMessageUtil.projectCancelMessage(expert.getExpertName(), project.getProjectName(), format, project.getProjectLocation());
        this.mailService.sendTextMailMessage(expert.getExpertEmail(), project.getProjectName()+"取消通知", cancelMessge);
    }
    @RabbitListener(queues = RabbitMQConstant.KEY_CANCEL_PROJECT_SYS)
    public void cancelProjectSys(String msg,Message message,Channel channel){
        JSONObject jsonObject = JSON.parseObject(msg);
        Object expertIdObject = jsonObject.get("expertId");
        Object projectIdObject = jsonObject.get("projectId");
        Long expertId = Long.parseLong(expertIdObject.toString());
        Long projectId = Long.parseLong(projectIdObject.toString());
        Expert expert = this.expertService.getById(expertId);
        Project project=this.projectService.getById(projectId);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format = dtf.format(project.getProjectReviewTime());
        String cancelMessge = EmailMessageUtil.projectCancelMessage(expert.getExpertName(), project.getProjectName(), format, project.getProjectLocation());
        Notice notice=new Notice();
        notice.setNoticeType(NoticeType.USER.getCode());
        notice.setNoticeTitle(project.getProjectName()+"取消通知");
        createNotice(expert, cancelMessge, notice);
    }


    @RabbitListener(queues =RabbitMQConstant.KEY_RE_EXTRACT)
    public  void reExtractMessage(String msg,Message message,Channel channel){
        JSONObject jsonObject = JSON.parseObject(msg);
        Object expertIdObject = jsonObject.get("expertId");
        Object projectIdObject = jsonObject.get("projectId");
        Long expertId = Long.parseLong(expertIdObject.toString());
        Long projectId = Long.parseLong(projectIdObject.toString());
        Expert expert = this.expertService.getById(expertId);
        Project project=this.projectService.getById(projectId);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format = dtf.format(project.getProjectReviewTime());
          String reExtractMessage= EmailMessageUtil.projectReExtractMessage(expert.getExpertName(),project.getProjectName(),format,project.getProjectLocation());
          this.mailService.sendTextMailMessage(expert.getExpertEmail(),project.getProjectName()+"取消评审通知",reExtractMessage);
          Notice notice=new Notice();
        notice.setNoticeTitle(project.getProjectName()+"取消评审通知");
        createNotice(expert, reExtractMessage, notice);
    }

    @RabbitListener(queues = RabbitMQConstant.KEY_CANCEL_EXTRACT_RESULT)
    public void cancelExtractResult(String msg,Message message,Channel channel){
        JSONObject jsonObject = JSON.parseObject(msg);
        Object expertIdObject = jsonObject.get("expertId");
        Object projectIdObject = jsonObject.get("projectId");
        Long expertId = Long.parseLong(expertIdObject.toString());
        Long projectId = Long.parseLong(projectIdObject.toString());
        Expert expert = this.expertService.getById(expertId);
        Project project=this.projectService.getById(projectId);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format = dtf.format(project.getProjectReviewTime());
       String cancelExtractMessage= EmailMessageUtil.projectCancelExtractMessage(expert.getExpertName(),project.getProjectName(),format,project.getProjectLocation());
        this.mailService.sendTextMailMessage(expert.getExpertEmail(),project.getProjectName()+"取消抽取结果通知",cancelExtractMessage);
        Notice notice=new Notice();
        notice.setNoticeTitle(project.getProjectName()+"取消评审通知");
        createNotice(expert, cancelExtractMessage, notice);
    }
    private void createNotice(Expert expert, String reExtractMessage, Notice notice) {
        notice.setNoticeContent(reExtractMessage);
        setNoticeInfo(expert, notice);
    }

    private void setNoticeInfo(Expert expert, Notice notice) {
        notice.setUpdateTime(LocalDateTime.now());
        notice.setCreateTime(LocalDateTime.now());
        notice.setCreateUser(1L);
        notice.setUpdateUser(1L);
        this.noticeService.save(notice);
        NoticeUser noticeUser=new NoticeUser();
        noticeUser.setNoticeId(notice.getId());
        noticeUser.setUserId(expert.getExpertUser());
        this.noticeUserService.save(noticeUser);
    }
}

